package com.lgy.controller;

import com.github.pagehelper.PageInfo;
import com.lgy.pojo.Customer;
import com.lgy.pojo.Employee;
import com.lgy.service.ICustomerService;
import com.lgy.service.IEmployeeService;
import com.lgy.util.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/customer")
public class CustomerController {
    @Autowired
    private ICustomerService customerService;
    @Autowired
    private IEmployeeService employeeService;
    //customer/getAllCustomer.do
    @RequestMapping("/getAllCustomer.do")
    public String getAllCustomer(Model model, HttpSession session,@RequestParam(value = "page",defaultValue = "1") Integer page,@RequestParam(value = "pageSize",defaultValue = "3")Integer pageSize){
        Employee emp = (Employee) session.getAttribute("emp");
        PageInfo pageInfo = customerService.findAllById(emp.getCompanyid(), page, pageSize);
        model.addAttribute("pageInfo",pageInfo);
        return "EmpCustomerList";
    }

    @RequestMapping("/getAdminAllCustomer.do")
    public String getAdminAllCustomer(Model model,HttpSession session,@RequestParam(value = "page",defaultValue = "1") Integer page,@RequestParam(value = "pageSize",defaultValue = "3")Integer pageSize){
        Employee emp = (Employee) session.getAttribute("emp");
        PageInfo pageInfo = customerService.findAllById(emp.getCompanyid(), page, pageSize);
        model.addAttribute("pageInfo",pageInfo);
        return "AdminCustomerList";
    }
    //insertCustomer.do添加客户
    @RequestMapping(value = "/insertCustomer.do",method = RequestMethod.POST)
    public String insertEmployee(Model model,Customer customer,Integer empId) {
        customer.setCustomerCreateTime(new Date());
        int row = customerService.insert(customer);
        Employee employee = employeeService.findByEmpId(empId);
        if(employee!=null){
            if(employee.getPositionid()==1){
                return "redirect:/customer/getAdminAllCustomer.do";
            }
        }
        return "forward:/customer/getAllCustomer.do";
    }

    //getCustomerPhoneById.do
    @RequestMapping("/getCustomerPhoneById.do")
    @ResponseBody
    public ResultData getCustomerPhoneById(HttpSession session,@RequestParam("customerPhone")String customerPhone){
        Employee emp = (Employee) session.getAttribute("emp");
        Customer customer = customerService.findByPhone(customerPhone,emp.getCompanyid());
        if(customer!=null){
            return ResultData.ok(true);
        }
        return ResultData.error("错误");
    }

    //删除客户
    @RequestMapping("/deleteCustomerById.do")
    public String deleteCustomerById(@RequestParam("customerId")Integer customerId,@RequestParam("empId")Integer empId) {
        Employee employee = employeeService.findByEmpId(empId);
        customerService.deleteByPrimaryKey(customerId);
        if(employee.getPositionid()==1){
            return "redirect:/customer/getAdminAllCustomer.do";
        }
        return "redirect:/customer/getAllCustomer.do";
    }


    //修改
    @RequestMapping("/getCustomerById.do")
    @ResponseBody
    public ResultData<Customer> getCustomerById(@RequestParam("customerId")Integer customerId) {
        Customer customer = customerService.selectByPrimaryKey(customerId);
        if(customer!=null){
            return ResultData.ok(customer);
        }
        return ResultData.error("获取失败");
    }
    //system/updateEmployee.do
    @RequestMapping(value = "/updateCustomer.do",method = RequestMethod.POST)
    public String updateCustomer(Customer customer,@RequestParam("empId")Integer empId) {
        Employee employee = employeeService.findByEmpId(empId);
        customerService.updateByPrimaryKeySelective(customer);
        if(employee.getPositionid()==1){
            return "redirect:/customer/getAdminAllCustomer.do";
        }
        return "redirect:/customer/getAllCustomer.do";
    }
}
